package com.atguigu.component;

import com.atguigu.bean.Employee;
import com.atguigu.dao.DepartmentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;

/**
 * 1、实现Converter接口，是org.springframework.core.convert.converter.Converter【不要导错了】
 * 2、创建的时候，传递2个泛型Converter<S,T>
 *     S是source来源
 *     T是target目标
 * @author 伟哥
 * @date 2021-05-19 17:59
 */
public class MyStringToEmployeeConverter implements Converter<String, Employee>{
    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 自定义的转换规则
     * @param source
     * @return
     */
    @Override
    public Employee convert(String source) {
        //empAdmin-admin@qq.com-1-101
        System.out.println("这是页面提交的字符串："+source);

        Employee employee = new Employee();
        /*
        source.contains("-")
        source里面是否，包含"-"
         */
        if (source.contains("-")){
            String[] split = source.split("-");
            employee.setLastName(split[0]);
            employee.setEmail(split[1]);
            employee.setGender(Integer.parseInt(split[2]));
            employee.setDepartment(departmentDao.getDepartment(Integer.parseInt(split[3])));
        }
        return employee;
    }
}
